package com.lineage.server.timecontroller.event.ranking;

import com.lineage.server.model.L1Clan;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.WorldClan;
import java.util.Collection;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class RankingClanTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(RankingClanTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  private static boolean _load = false;
  
  private static String[] _clanName = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
  
  public void start() {
    int timeMillis = 600000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 600000L, 600000L);
  }
  
  public static String[] userName() {
    if (!_load)
      load(); 
    return _clanName;
  }
  
  public void run() {
    try {
      load();
    } catch (Exception e) {
      _log.error("血盟风云榜时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      RankingClanTimer clanRTimer = new RankingClanTimer();
      clanRTimer.start();
    } 
  }
  
  private static void load() {
    try {
      Collection<L1Clan> allClan = WorldClan.get().getAllClans();
      if (allClan.isEmpty())
        return; 
      _load = true;
      restart();
      Iterator<L1Clan> iter = allClan.iterator();
      while (iter.hasNext()) {
        L1Clan clan = iter.next();
        String clanName = clan.getClanName();
        if (clanName.equalsIgnoreCase("管理团队"))
          continue; 
        int count = clan.getOnlineClanMemberSize();
        _clanName = intTree(count, clanName, _clanName);
        Thread.sleep(1L);
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private static void restart() {
    _clanName = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
  }
  
  private static String[] intTree(int count, String clanName, String[] userName) {
    if (userName[0].equals(" ")) {
      userName[0] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    String[] set = userName[0].split(",");
    int srcCount = Integer.parseInt(set[1]);
    if (srcCount < count && !set[0].equals(clanName)) {
      userName[9] = userName[8];
      userName[8] = userName[7];
      userName[7] = userName[6];
      userName[6] = userName[5];
      userName[5] = userName[4];
      userName[4] = userName[3];
      userName[3] = userName[2];
      userName[2] = userName[1];
      userName[1] = userName[0];
      userName[0] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    if (userName[1].equals(" ")) {
      userName[1] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    set = userName[1].split(",");
    srcCount = Integer.parseInt(set[1]);
    if (srcCount < count && !set[0].equals(clanName)) {
      userName[9] = userName[8];
      userName[8] = userName[7];
      userName[7] = userName[6];
      userName[6] = userName[5];
      userName[5] = userName[4];
      userName[4] = userName[3];
      userName[3] = userName[2];
      userName[2] = userName[1];
      userName[1] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    if (userName[2].equals(" ")) {
      userName[2] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    set = userName[2].split(",");
    srcCount = Integer.parseInt(set[1]);
    if (srcCount < count && !set[0].equals(clanName)) {
      userName[9] = userName[8];
      userName[8] = userName[7];
      userName[7] = userName[6];
      userName[6] = userName[5];
      userName[5] = userName[4];
      userName[4] = userName[3];
      userName[3] = userName[2];
      userName[2] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    if (userName[3].equals(" ")) {
      userName[3] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    set = userName[3].split(",");
    srcCount = Integer.parseInt(set[1]);
    if (srcCount < count && !set[0].equals(clanName)) {
      userName[9] = userName[8];
      userName[8] = userName[7];
      userName[7] = userName[6];
      userName[6] = userName[5];
      userName[5] = userName[4];
      userName[4] = userName[3];
      userName[3] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    if (userName[4].equals(" ")) {
      userName[4] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    set = userName[4].split(",");
    srcCount = Integer.parseInt(set[1]);
    if (srcCount < count && !set[0].equals(clanName)) {
      userName[9] = userName[8];
      userName[8] = userName[7];
      userName[7] = userName[6];
      userName[6] = userName[5];
      userName[5] = userName[4];
      userName[4] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    if (userName[5].equals(" ")) {
      userName[5] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    set = userName[5].split(",");
    srcCount = Integer.parseInt(set[1]);
    if (srcCount < count && !set[0].equals(clanName)) {
      userName[9] = userName[8];
      userName[8] = userName[7];
      userName[7] = userName[6];
      userName[6] = userName[5];
      userName[5] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    if (userName[6].equals(" ")) {
      userName[6] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    set = userName[6].split(",");
    srcCount = Integer.parseInt(set[1]);
    if (srcCount < count && !set[0].equals(clanName)) {
      userName[9] = userName[8];
      userName[8] = userName[7];
      userName[7] = userName[6];
      userName[6] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    if (userName[7].equals(" ")) {
      userName[7] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    set = userName[7].split(",");
    srcCount = Integer.parseInt(set[1]);
    if (srcCount < count && !set[0].equals(clanName)) {
      userName[9] = userName[8];
      userName[8] = userName[7];
      userName[7] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    if (userName[8].equals(" ")) {
      userName[8] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    set = userName[8].split(",");
    srcCount = Integer.parseInt(set[1]);
    if (srcCount < count && !set[0].equals(clanName)) {
      userName[9] = userName[8];
      userName[8] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    if (userName[9].equals(" ")) {
      userName[9] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    set = userName[9].split(",");
    srcCount = Integer.parseInt(set[1]);
    if (srcCount < count && !set[0].equals(clanName)) {
      userName[9] = String.valueOf(String.valueOf(clanName)) + "," + count;
      return userName;
    } 
    return userName;
  }
}
